package com.wwwarehouse.financialcenter.fragment.moneywarehouse.billingrecords;


import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bill.BillRecordResponseBean;
import com.wwwarehouse.financialcenter.bean.bill.ChargingRecordBean;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.fragment.base.TheParentFragment;
import com.wwwarehouse.financialcenter.fragment.moneywarehouse.bills.BillOrderGoodsPagerFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wwwarehouse.common.constant.Constant.sp_User_Id;

/**
 * @author Joseph on 2017/11/15.
 *         <p/>
 *         记录详情
 */

public class ChargingRecordDetailsFragment extends TheParentFragment implements ElasticScrollView.OnPullListener {

    private ElasticScrollView mCustomScroll;
    private StateButton mode_button ;
    private RelativeLayout mOrderGoods ;    //跳转订购商品
    private RelativeLayout mOrderDetails ;  //跳转订单详情
    private TextView  mChargeRecordUkid;   //流水号
    private TextView  mChargeTypeDesc;     //计费项
    private TextView  mOtherSideName;      //供方
    private TextView  mRecordAmount;        //金额
    private TextView  mRecordDetail;        //备注
    private TextView  mChargeGeneratedTime;//时间
    private TextView  mRecordPayType;      //结算方式
    private TextView  mRecordStatusDesc;    //计费情况
    private TextView  mRelationContentUkid; //关联业务信息
    private TextView  mWhichSide ;//供方还是需方
    private String mBillId;
    private String mBusinessId;
    private int orderType ;
    private static final int REQUEST_LIST_CHARGE_RECORD = 0;
    private int chargeType; // 费用类型（0=订购商品，1=取消订购，2=仓储契约，3=劳务）
    private ChargingRecordBean.ListBean chargingRecordDetailsBean;

    @Override
    protected int getContentId() {
        mLoadingView.setVisibility(View.VISIBLE);
        return R.layout.financial_center_charging_record_details_fragment;
    }

    @Override
    public void setTitle() {
        if (peekFragment()instanceof ChargingRecordDetailsFragment){
            mActivity.setTitle(getString(R.string.financial_center_record_details));
        }
    }

    @Override
    protected void init(View view) {
        mode_button = (StateButton) view.findViewById(R.id.btn_state);
        mOrderGoods = (RelativeLayout) view.findViewById(R.id.order_goods);
        mOrderDetails = (RelativeLayout) view.findViewById(R.id.order_details);
        mCustomScroll = (ElasticScrollView) view.findViewById(R.id.ecl_custom);
        mCustomScroll.setOnPullListener(this);
        mChargeRecordUkid = findView(view,R.id.charge_record_ukid);
        mChargeTypeDesc = findView(view,R.id.charge_type_desc);
        mOtherSideName = findView(view,R.id.other_side_name);
        mRecordAmount = findView(view,R.id.record_amount);
        mRecordDetail = findView(view,R.id.record_detail);
        mChargeGeneratedTime = findView(view,R.id.charge_generated_time);
        mRecordPayType = findView(view,R.id.record_pay_type);
        mRecordStatusDesc = findView(view,R.id.record_status_desc);
        mRelationContentUkid = findView(view,R.id.relation_content_ukid);
        mWhichSide = findView(view,R.id.which_side);
    }

    @Override
    protected void loadDatas() {
        Bundle bundle = getArguments();
        if (bundle!=null){
                     mBusinessId =bundle.getString("businessId");
            chargingRecordDetailsBean=  bundle.getParcelable("data");

            if (chargingRecordDetailsBean != null) {
                mLoadingView.setVisibility(View.GONE);
                setData();
            }

            BillRecordResponseBean.BillRecordDetailsBean billRecordDetailsBean = bundle.getParcelable("details");
            if (billRecordDetailsBean != null) {
                mBillId = billRecordDetailsBean.getBillUkid();
                mBusinessId = billRecordDetailsBean.getBusinessId();
                getListChargeRecordByBillId();
            }
        }
    }

    private void setData() {
        chargeType = chargingRecordDetailsBean.getChargeType();
        mChargeRecordUkid.setText(chargingRecordDetailsBean.getChargeRecordUkid());
        mChargeTypeDesc.setText(chargingRecordDetailsBean.getChargeTypeDesc());
        mOtherSideName.setText(chargingRecordDetailsBean.getOtherSideName());
        if ("supply".equals(chargingRecordDetailsBean.getWhichSide())) {  //供方  卖家  收钱
            orderType = 2;
            mWhichSide.setText(R.string.financial_center_demand);
            mRecordAmount.setTextColor(mActivity.getResources().getColor(R.color.common_color_c1_337cff));
            mRecordAmount.setText(chargingRecordDetailsBean.getRecordAmount());
        } else if ("demand".equals(chargingRecordDetailsBean.getWhichSide())) {  //需方 买家  掏钱
            orderType = 1;
            mWhichSide.setText(R.string.financial_center_supply);
            mRecordAmount.setTextColor(mActivity.getResources().getColor(R.color.common_color_c2_f7a82f));
            mRecordAmount.setText(chargingRecordDetailsBean.getRecordAmount());
        } else {

        }

        mRecordDetail.setText(chargingRecordDetailsBean.getRecordDetail());
        mChargeGeneratedTime .setText(chargingRecordDetailsBean.getChargeGeneratedTime());
        mRecordPayType .setText(chargingRecordDetailsBean.getRecordPayTypeDesc());

        mRecordStatusDesc .setText(chargingRecordDetailsBean.getRecordStatusDesc());

        if ("0".equals(chargingRecordDetailsBean.getRecordStatus())) {
            // 未入账
            mRecordStatusDesc.setTextColor(mActivity.getResources().getColor(R.color.common_color_c1_337cff));
        } else {

        }

        mRelationContentUkid.setText(chargeType==1?"终止单号"+chargingRecordDetailsBean.getRelationContentUkid():"订单号"+chargingRecordDetailsBean.getRelationContentUkid());
        mOrderGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("contractUkid", chargingRecordDetailsBean.getRelationContentUkid());
                pushFragment("/contract/BuyGoodDetailViewPagerFragment",bundle);
            }
        });
        mOrderDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (chargeType==0){
                    Bundle bundle = new Bundle();
                    bundle.putInt("orderType",orderType);
                    bundle.putString("contractUkid", chargingRecordDetailsBean.getRelationContentUkid());
                    pushFragment("/contract/BuyOrderDetailsFragment",bundle);

                }else {
                    Bundle bundle = new Bundle();
                    bundle.putString("businessId",mBusinessId);
                    bundle.putString("contractUkid", chargingRecordDetailsBean.getRelationContentUkid());
                    pushFragment("/contract/ConfirmTerminateOrderFragment",bundle);
                }

            }
        });
    }

    private void getListChargeRecordByBillId() {
        Map map = new HashMap();

        map.put("billId", mBillId);
        map.put("businessId", mBusinessId);
        map.put("userId", sp.getValue(sp_User_Id));

        httpPost(FinancialConstant.URL_LIST_CHARGE_RECORD_BY_BILLID, map, REQUEST_LIST_CHARGE_RECORD, false, null);

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case REQUEST_LIST_CHARGE_RECORD:
                List<ChargingRecordBean.ListBean> chargingRecordDetailsList = JSON.parseArray(commonClass.getData().toString(), ChargingRecordBean.ListBean.class);
                chargingRecordDetailsBean = chargingRecordDetailsList.get(0);
                if (chargingRecordDetailsBean != null){
                    setData();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onReLoad(int code) {
        getListChargeRecordByBillId();
    }

    @Override
    public void onDownPull() {

    }

    @Override
    public void onUpPull() {
//        mode_button.setVisibility(isShow?View.VISIBLE:View.GONE);
//        isShow = !isShow;
    }
}
